<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="接口名称" prop="interName">
        <el-input
          v-model="queryParams.interName"
          placeholder="请输入接口名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接口编号" prop="interCode">
        <el-input
          v-model="queryParams.interCode"
          placeholder="请输入接口编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="项目" prop="interOfpro">
        <el-input
          v-model="queryParams.interOfpro"
          placeholder="请输入项目"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="项目" prop="interOfpro">
        <el-select
          v-model="queryParams.interOfpro"
          placeholder="项目列表"
          clearable
          size="small"
        >
          <el-option
            v-for="pro in simpleProList"
            :key="pro.id"
            :label="pro.proName"
            :value="pro.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dmsIms:interface:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dmsIms:interface:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dmsIms:interface:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dmsIms:interface:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="interfaceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="接口id" align="center" prop="id" width="60" />
      <el-table-column label="接口名称" align="center" prop="interName" />
      <el-table-column label="接口编号" align="center" prop="interCode" />
      <el-table-column label="接口类型" align="center" prop="interType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.interType === '1'" type="success">GET</el-tag>
          <el-tag v-else-if="scope.row.interType === '2'">POST</el-tag>
          <el-tag v-else-if="scope.row.interType === '3'" type="warning"
            >PUT</el-tag
          >
          <el-tag v-else-if="scope.row.interType === '4'" type="info"
            >PATCH</el-tag
          >
          <el-tag v-else-if="scope.row.interType === '5'" type="danger"
            >DEL</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        label="接口路径"
        align="center"
        prop="interPath"
        show-overflow-tooltip
      />
      <el-table-column
        label="接口头参数"
        align="center"
        prop="interParamHeader"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-caret-right"
            @click="handleExecute(scope.row)"
            >执行</el-button
          > -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleDetail(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dmsIms:interface:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dmsIms:interface:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目接口对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="接口名称" prop="interName">
          <el-input v-model="form.interName" placeholder="请输入接口名称" />
        </el-form-item>
        <el-form-item label="接口编号" prop="interCode">
          <el-input v-model="form.interCode" placeholder="请输入接口编号" />
        </el-form-item>
        <el-form-item label="接口类型" prop="interType">
          <el-radio-group v-model="form.interType">
            <el-radio
              v-for="dict in dict.type.pro_inter_type"
              :key="dict.value"
              :label="dict.value"
            >
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
          <!-- <el-input v-model="form.interType" placeholder="请输入接口类型"></el-input> -->
        </el-form-item>
        <el-form-item label="接口路径" prop="interPath">
          <el-input v-model="form.interPath" placeholder="请输入接口路径" />
        </el-form-item>
        <el-form-item label="接口头参数" prop="interParamHeader">
          <el-input
            v-model="form.interParamHeader"
            type="textarea"
            placeholder="content-type: text/xml"
            :autosize="{ minRows: 2, maxRows: 2 }"
          />
        </el-form-item>
        <el-form-item label="接口体参数">
          <el-radio-group v-model="form.interParamBody.bodyType">
            <el-radio label="none" value="none"></el-radio>
            <el-radio label="formData" value="formData"></el-radio>
            <el-radio label="raw" value="raw"></el-radio>
          </el-radio-group>
          <el-input
            v-model="form.interParamBody.bodyData"
            type="textarea"
            placeholder="请输入接口体参数"
            :autosize="{ minRows: 3, maxRows: 3 }"
          />
        </el-form-item>
        <el-form-item label="预期结果" prop="interResult">
          <el-input
            v-model="form.interResult"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="实际结果" prop="interRelResult">
          <el-input
            v-model="form.interRelResult"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="接口所属项目" prop="interOfpro">
          <el-select
            v-model="form.interOfpro"
            placeholder="项目列表"
            clearable
            size="small"
          >
            <el-option
              v-for="pro in simpleProList"
              :key="pro.id"
              :label="pro.proName"
              :value="pro.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看项目接口信息 -->
    <el-dialog
      :title="title"
      :visible.sync="searchOpen"
      width="680px"
      append-to-body
    >
      <el-descriptions
        class="margin-top"
        :title="form.interName"
        :column="1"
        size="medium"
        :border="true"
        :labelStyle="{ width: '120px' }"
      >
        <el-descriptions-item label="接口名称">
          {{ form.interName }}
        </el-descriptions-item>
        <el-descriptions-item label="接口编号">
          {{ form.interCode }}
        </el-descriptions-item>
        <el-descriptions-item label="接口类型">
          <el-tag v-if="form.interType === '1'" type="success">GET</el-tag>
          <el-tag v-else-if="form.interType === '2'">POST</el-tag>
          <el-tag v-else-if="form.interType === '3'" type="warning">PUT</el-tag>
          <el-tag v-else-if="form.interType === '4'" type="info">PATCH</el-tag>
          <el-tag v-else-if="form.interType === '5'" type="danger">DEL</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="接口路径">
          {{ form.interPath }}
        </el-descriptions-item>
        <el-descriptions-item label="接口头参数">
          <el-input
            v-model="form.interParamHeader"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 2 }"
            disabled
          />
        </el-descriptions-item>
        <el-descriptions-item label="接口体参数">
          <el-tag v-if="form.interParamBody.bodyType === 'none'" type="info"
            >none</el-tag
          >
          <el-tag v-else-if="form.interParamBody.bodyType === 'formData'"
            >form-data</el-tag
          >
          <el-tag v-else type="success">raw</el-tag>
          <el-divider></el-divider>
          <el-input
            v-model="form.interParamBody.bodyData"
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 3 }"
            disabled
          />
        </el-descriptions-item>
        <el-descriptions-item label="接口结果">
          <el-input
            v-model="form.interResult"
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 3 }"
            disabled
          />
        </el-descriptions-item>
        <el-descriptions-item label="接口实际结果">
          <el-input
            v-model="form.interRelResult"
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 3 }"
            disabled
          />
        </el-descriptions-item>
        <el-descriptions-item label="所属项目">
          <el-select
            v-model="form.interOfpro"
            placeholder="项目列表"
            clearable
            size="small"
            disabled
          >
            <el-option
              v-for="pro in simpleProList"
              :key="pro.id"
              :label="pro.proName"
              :value="pro.id"
            >
            </el-option>
          </el-select>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 执行接口弹出框 -->
    <!-- <el-dialog
      :title="title"
      :visible.sync="excuteOpen"
      width="680px"
      append-to-body
    >
      <InterfaceExecute />
    </el-dialog> -->
  </div>
</template>

<script>
import {
  listInterface,
  getInterface,
  delInterface,
  addInterface,
  updateInterface,
} from "@/api/dmsIms/interface";
import { mapActions, mapState } from "vuex";
// import InterfaceExecute from "./execute";

export default {
  name: "Interface",
  dicts: ["pro_inter_type"],
  // components: {
  //   InterfaceExecute,
  // },
  computed: {
    // vuex中的state方法简写方式
    ...mapState({
      simpleProList: (state) => state.prolist.simpleProList,
    }),
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目接口表格数据
      interfaceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      searchOpen: false,
      // excuteOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        interName: null,
        interCode: null,
        interType: null,
        interOfpro: null,
      },
      // 表单参数
      form: {
        interParamBody: {
          bodyType: "none",
          bodyData: "",
        },
      },
      // 表单校验
      rules: {
        interName: [
          { required: true, message: "接口名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    // 如果项目列表数据为空，则去获取项目列表
    if (this.simpleProList.length <= 0) {
      this.getProList();
    }
    this.getList();
  },
  methods: {
    // vuex中的actions方法简写方式
    ...mapActions(["getProList"]),
    /** 查询项目接口列表 */
    getList() {
      this.loading = true;
      listInterface(this.queryParams).then((response) => {
        this.interfaceList = response.rows;
        let index = 0;
        this.interfaceList.forEach((elem) => {
          elem.interParamBody = JSON.stringify(elem.interParamBody);
          this.interfaceList[index].interParamBody = JSON.parse(
            elem.interParamBody
          );
          index += 1;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        interName: null,
        interCode: null,
        interType: "1",
        interPath: null,
        interParamHeader: null,
        interParamBody: {
          bodyType: "none",
          bodyData: "",
        },
        interResult: null,
        interRelResult: null,
        interOfpro: null,
        status: 0,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 接口执行按钮 */
    // handleExecute() {
    //   this.excuteOpen = true;
    // },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 查询按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids;
      getInterface(id).then((response) => {
        this.form = response.data;
        // console.log(response);
        try {
          this.form.interParamBody = JSON.parse(this.form.interParamBody) || {
            bodyType: "none",
            bodyData: "",
          };
        } catch (error) {
          this.form.interParamBody = {
            bodyType: "none",
            bodyData: "",
          };
        }
        this.searchOpen = true;
        this.title = "接口信息";
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目接口";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInterface(id).then((response) => {
        this.form = response.data;
        try {
          this.form.interParamBody = JSON.parse(this.form.interParamBody) || {
            bodyType: "none",
            bodyData: "",
          };
        } catch (error) {
          this.form.interParamBody = {
            bodyType: "none",
            bodyData: "",
          };
        }
        this.open = true;
        this.title = "修改项目接口";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.interParamBody = JSON.stringify(this.form.interParamBody);
          if (this.form.id != null) {
            console.log(this.form);
            updateInterface(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInterface(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除项目接口编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInterface(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "dmsIms/interface/export",
        {
          ...this.queryParams,
        },
        `interface_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
